﻿using Client.Constant;
using Client.Framework.Message;
using Client.Model;
using Client.Service;
using Client.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.Message.Response
{
    [MessageAttr(MessageId.RESPONSE_LOGIN)]
    class LoginResp : ResponseBaseMessage
    {
        public int Code { get; set; }
        public string ErrMsg { get; set; }

        public ViewUser LoginUser { get; set; }


        public override void Deserialize(byte[] buffer)
        {
            ResponseLogin msg = ResponseLogin.Parser.ParseFrom(buffer);
            Code = msg.Code;
            ErrMsg = msg.ErrMsg;
            if (msg.LoginUser != null)
            {
                LoginUser = new ViewUser()
                {
                    ID = msg.LoginUser.Id,
                    LoginName = msg.LoginUser.LoginName,
                    OrgId = msg.LoginUser.OrgId,
                    OrgName = OrgService.Instance().GetOrgById(msg.LoginUser.OrgId) == null ? string.Empty : OrgService.Instance().GetOrgById(msg.LoginUser.OrgId).OrgName,
                    Password = MD5Util.MD5Decrypt(msg.LoginUser.Password),
                    // 前端暂时取第一个角色
                    Role = msg.LoginUser.RoleIds.Count > 0 ? RoleService.Instance().GetRoleById(msg.LoginUser.RoleIds[0]) : null
                };
            }
        }
    }
}
